//
//  RoothomeTableViewCell.m
//  F50
//
//  Created by zack on 15/5/19.
//  Copyright (c) 2015年 ZACK. All rights reserved.
//

#import "RoothomeTableViewCell.h"
@implementation RoothomeTableViewCell
{
    int relations;
    int isrelation;
    NSString *projectID;
}
- (void)awakeFromNib {
    // Initialization code
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    
        
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(10, 5, WIDTH - 20, 115)];
        bgView.backgroundColor = RGB(54, 101, 144);
        bgView.layer.cornerRadius = 5;
        [self.contentView addSubview:bgView];
        
        
        
        self.photoImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 70, 70)];
        
        self.photoImage.layer.cornerRadius = 35;
        self.photoImage.layer.masksToBounds = YES;
        [bgView addSubview:self.photoImage];
        
        self.nameLab = [[UILabel alloc]initWithFrame:CGRectMake(80, 10, 180, 30)];
        self.nameLab.font = [UIFont systemFontOfSize:18];
        self.nameLab.textColor = [UIColor whiteColor];
        [bgView addSubview:self.nameLab];
        
        self.timeLab = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH - 110, 5, 80, 20)];
        self.timeLab.font = [UIFont systemFontOfSize:14];
        self.timeLab.textAlignment = NSTextAlignmentRight;
        self.timeLab.textColor = [UIColor whiteColor];
        [bgView addSubview:self.timeLab];
        
        self.infoLab = [[UILabel alloc]initWithFrame:CGRectMake(80, 40, WIDTH - 20 - 80 - 10, 20)];
        self.infoLab.font = [UIFont systemFontOfSize:14];
        self.infoLab.textAlignment = NSTextAlignmentLeft;
        self.infoLab.textColor = RGB(218, 212, 222);
        [bgView addSubview:self.infoLab];
        
        
        UIImageView *syView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 62, 15, 15)];
        syView.image = [UIImage imageWithImageName:@"symbol"];
         [bgView addSubview:syView];
        
        self.stateLab = [[UILabel alloc]initWithFrame:CGRectMake(100, 60, WIDTH - 20 - 80 - 10, 20)];
        self.stateLab.font = [UIFont systemFontOfSize:15];
        self.stateLab.textAlignment = NSTextAlignmentLeft;
        self.stateLab.textColor = RGB(218, 212, 222);
        [bgView addSubview:self.stateLab];
        
        
        
        UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, 85, WIDTH - 20, 1)];
        line.backgroundColor = RGB(107, 137, 163);
        [bgView addSubview:line];
        
        
        UIImageView *lockView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 92, 15, 15)];
        lockView.image = [UIImage imageWithImageName:@"lock"];
        if ([[USER_DEFAULT objectForKey:@"iscertificated"]intValue] == 2) {
            lockView.image = [UIImage imageWithImageName:@"dollar@2x"];
        }
        [bgView addSubview:lockView];
        
        self.powerLab = [[UILabel alloc]initWithFrame:CGRectMake(22, 90, 200, 20)];
        self.powerLab.font = [UIFont systemFontOfSize:15];
        self.powerLab.textAlignment = NSTextAlignmentLeft;
        self.powerLab.textColor = RGB(218, 212, 222);
        [bgView addSubview:self.powerLab];
        
        
        self.favourite = [UIButton buttonWithType:UIButtonTypeCustom];
        self.favourite.frame = CGRectMake(WIDTH - 120 , 90, 40, 20);
       
        self.favourite.titleLabel.font = [UIFont systemFontOfSize:14];
        self.favourite.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        self.favourite.layer.borderColor = RGB(186, 183, 183).CGColor;
        self.favourite.layer.borderWidth = 1;
        self.favourite.layer.masksToBounds = YES;
            [self.favourite addTarget:self action:@selector(doFavourite) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:self.favourite];
        
        
        self.chat = [UIButton buttonWithType:UIButtonTypeCustom];
        self.chat.frame = CGRectMake(WIDTH - 70 , 90, 40, 20);
        [self.chat setImage:[UIImage imageWithImageName:@"chatForBtn@2x"] forState:UIControlStateNormal];
//        [self.chat setTitle:@"4" forState:UIControlStateNormal];
        self.chat.titleLabel.font = [UIFont systemFontOfSize:14];
        self.chat.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        self.chat.layer.borderColor = RGB(186, 183, 183).CGColor;
        self.chat.layer.borderWidth = 1;
        self.chat.layer.masksToBounds = YES;
        [bgView addSubview:self.chat];
        
        
        
        
    }
    return self;

}


- (void)setCellLableText:(RoothomeDataModle *)modle{
    
    [self.photoImage sd_setImageWithURL:[NSURL URLWithString:modle.imageUrl] placeholderImage:[UIImage imageNamed:@"profile"]];
    
    self.nameLab.text = modle.title;
    self.timeLab.text = modle.releasetime;
    self.infoLab.text = modle.info;
    self.stateLab.text = modle.state;
    self.powerLab.text = @"投资人可见";
    
    [self.chat setTitle:modle.deals forState:UIControlStateNormal];
    relations = [modle.relations intValue];
    isrelation = [modle.isrelation intValue];

    if (isrelation == 1) {
        [self.favourite setImage:[UIImage imageWithImageName:@"favourite_checked@2x"] forState:UIControlStateNormal];
        
    }else{
         [self.favourite setImage:[UIImage imageWithImageName:@"faovurite@2x"] forState:UIControlStateNormal];
    }
    projectID = modle.projectID;
    [self.favourite setTitle:modle.relations forState:UIControlStateNormal];

    // 投资人已经被认证
    if ([[USER_DEFAULT objectForKey:@"iscertificated"]intValue] == 2) {
        self.powerLab.text = modle.amount;
    }
    
    
}
- (void)doFavourite{
//    if ( [[USER_DEFAULT objectForKey:@"iscertificated"] intValue] == 1) {
//        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有进行认证" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//        [alertView show];
//        return;
//    }
    NSDictionary * date = [[NSDictionary  alloc]initWithObjectsAndKeys:[USER_DEFAULT objectForKey:@"token"],@"token",projectID,@"id",nil];
    
    switch (isrelation) {
   
        case 0:
        {
            isrelation = 1;
            
            
            [self.favourite setTitle:[NSString stringWithFormat:@"%d",[self.favourite.titleLabel.text intValue] + 1] forState:UIControlStateNormal];
            [self.favourite setImage:[UIImage imageWithImageName:@"favourite_checked@2x"] forState:UIControlStateNormal];
            
            
         
            NSDictionary *pram = [[NSDictionary alloc]initWithObjectsAndKeys:@"relation",@"class",@"relation",@"func",date,@"param", nil];
            
            [[AFCustomObject shareAFCustomObject] doRequestActionWithParams:pram success:^(NSDictionary *result) {
                NSLog(@"%@",[result objectForKey:@"errMsg"]);
                NSLog(@"%@",result);
                
                NSString *newRelation = [NSString stringWithFormat:@"%d",[[USER_DEFAULT objectForKey:@"relations"] intValue] +1];
                [USER_DEFAULT setValue:newRelation forKey:@"relations"];
                
                self.blocks(isrelation);
                
            } fail:^{
                
            }];
            
            
        }
            break;
        case 1:
        {
           
            isrelation = 0;
            
            [self.favourite setTitle:[NSString stringWithFormat:@"%d",[self.favourite.titleLabel.text intValue] - 1] forState:UIControlStateNormal];
            [self.favourite setImage:[UIImage imageWithImageName:@"faovurite@2x"] forState:UIControlStateNormal];
            
            
            NSDictionary *pram = [[NSDictionary alloc]initWithObjectsAndKeys:@"relation",@"class",@"remove",@"func",date,@"param", nil];
            
            [[AFCustomObject shareAFCustomObject] doRequestActionWithParams:pram success:^(NSDictionary *result) {
                NSLog(@"%@",[result objectForKey:@"errMsg"]);
                NSLog(@"%@",result);
                
                NSString *newRelation = [NSString stringWithFormat:@"%d",[[USER_DEFAULT objectForKey:@"relations"] intValue] -1];
                [USER_DEFAULT setValue:newRelation forKey:@"relations"];
                
                self.blocks(isrelation);

                
            } fail:^{
                
            }];
            
            
        }
            break;
            
        default:
            break;
    }
   
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
